package com.example.fengyungo.service.impl;

import com.example.fengyungo.constant.MessageListConstant;
import com.example.fengyungo.dao.DriverDao;
import com.example.fengyungo.dao.MessageDao;
import com.example.fengyungo.dao.UserDao;
import com.example.fengyungo.domain.pojo.*;
import com.example.fengyungo.service.MessageService;
import com.example.fengyungo.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    MessageDao messageDao;

    @Autowired
    DriverDao driverDao;

    @Autowired
    UserDao userDao;

    /**
     * 根据司机id获取消息列表中的用户
     *
     * @param driverId 司机id
     * @return 司机消息列表中的用户
     */
    @Override
    public List<DriverMessageList> getMessageListByDriverId(String driverId) {
        List<DriverMessageList> users = messageDao.getMessageListByDriverId(driverId);
        return users;
    }

    /**
     * 根据司机id和用户id获取消息
     *
     * @param driverId 司机id
     * @param userId   用户id
     * @param num      聊天记录条数
     * @return List<Message>
     */
    @Override
    public List<Message> getMessagesByDriverIdAndUserId(String driverId, String userId, int num) {
        List<Message> messages = messageDao.getMessagesByDriverIdAndUserId(driverId, userId, num);
        return messages;
    }

    /**
     * 更新消息列表中的最新消息以及消息发送时间
     *
     * @param message Message
     * @return Integer
     */
    @Override
    public Integer updateDriverLastContentAndTime(Message message) {
        Integer res = messageDao.updateDriverLastContentAndTime(message);
        return res;
    }

    /**
     * 插入消息
     *
     * @param message Message
     * @return Integer
     */
    @Override
    public Integer insertMessage(Message message) {
        Integer res = messageDao.insertMessage(message);
        return res;
    }

    /**
     * 更新司机消息列表中的司机未读数
     *
     * @param driverId     司机id
     * @param userId       用户id
     * @param driverUnread 司机未读数
     * @return Integer
     */
    @Override
    public Integer updateDriverUnread(String driverId, String userId, Integer driverUnread) {
        Integer res = messageDao.updateDriverUnread(driverId, userId, driverUnread);
        return res;
    }

    /**
     * 根据用户id获取用户的消息列表
     *
     * @param userId 用户id
     * @return 用户消息列表
     */
    @Override
    public List<UserMessageList> getMessageListByUserId(String userId) {
        List<UserMessageList> userMessageLists = messageDao.getMessageListByUserId(userId);
        return userMessageLists;
    }

    /**
     * 更新用户消息列表中的最新消息以及消息发送时间
     *
     * @param message Message
     * @return Integer
     */
    @Override
    public Integer updateUserLastContentAndTime(Message message) {
        Integer res = messageDao.updateUserLastContentAndTime(message);
        return res;
    }

    /**
     * 更新用户消息列表中的司机未读数
     *
     * @param driverId   司机id
     * @param userId     用户id
     * @param userUnread 用户未读数
     * @return Integer
     */
    @Override
    public Integer updateUserUnread(String driverId, String userId, Integer userUnread) {
        Integer res = messageDao.updateUserUnread(userId, driverId, userUnread);
        return res;
    }

    /**
     * 获取司机未读数
     *
     * @param driverId 司机id
     * @param userId   用户id
     * @return Integer
     */
    @Override
    public Integer getDriverUnread(String driverId, String userId) {
        Integer driverUnread = messageDao.getDriverUnread(driverId, userId);
        return driverUnread;
    }

    /**
     * 获取用户未读数
     *
     * @param driverId 司机id
     * @param userId   用户id
     * @return Integer
     */
    @Override
    public Integer getUserUnread(String driverId, String userId) {
        Integer userUnread = messageDao.getUserUnread(userId, driverId);
        return userUnread;
    }

    /**
     * 司机与用户发起聊天
     *
     * @param userId   用户id
     * @param driverId 司机id
     * @return Integer
     */
    @Override
    public Boolean addMessageList(String userId, String driverId) {
        // 查询司机信息
        Driver driver = driverDao.getDriverById(driverId);
        // 查询用户信息
        User user = userDao.getUserById(userId);
        // listId
        int indexOfDriver = driverId.length() / 2;
        int indexOfUser = userId.length() / 2;
        String listId = CommonUtils.initIdByNowTime() + driverId.substring(indexOfDriver) + userId.substring(indexOfUser);
        String now = CommonUtils.getNowTime();
        // 封装DriverMessageList
        DriverMessageList driverMessageList = new DriverMessageList();
        driverMessageList.setListId(listId);
        driverMessageList.setDriverId(driverId);
        driverMessageList.setDriverListStatus(MessageListConstant.EXISTENCE);
        driverMessageList.setDriverUnread(0);
        driverMessageList.setUserId(userId);
        driverMessageList.setUserAvatar(user.getUserAvatar());
        driverMessageList.setUserNickname(user.getUserNickname());
        driverMessageList.setLastContent(MessageListConstant.EMPTY_STRING);
        driverMessageList.setLastTime(now);
        driverMessageList.setUserOnline(MessageListConstant.ONLINE);
        // 封装UserMessageList
        UserMessageList userMessageList = new UserMessageList();
        userMessageList.setListId(listId);
        userMessageList.setUserId(userId);
        userMessageList.setDriverId(driverId);
        userMessageList.setDriverNickname(driver.getNickname());
        userMessageList.setDriverAvatar(driver.getAvatar());
        userMessageList.setUserUnread(0);
        userMessageList.setUserListStatus(MessageListConstant.EXISTENCE);
        userMessageList.setLastContent(MessageListConstant.EMPTY_STRING);
        userMessageList.setLastTime(now);
        userMessageList.setDriverOnline(MessageListConstant.ONLINE);
        // 发起聊天
        Integer res1 = messageDao.addUserMessageList(userMessageList);
        Integer res2 = messageDao.addDriverMessageList(driverMessageList);
        return res1>0 && res2>0;
    }

    /**
     * 修改司机消息列表状态
     *
     * @param driverId         司机id
     * @param userId           用户id
     * @param driverListStatus 列表状态，0存在，1不存在
     * @return Integer
     */
    @Override
    public Integer updateDriverMessageListStatus(String driverId, String userId, Integer driverListStatus) {
        Integer res = messageDao.updateDriverMessageListStatus(driverId, userId, driverListStatus);
        return res;
    }

    /**
     * 修改用户消息列表状态
     *
     * @param driverId       司机id
     * @param userId         用户id
     * @param userListStatus 列表状态，0存在，1不存在
     * @return Integer
     */
    @Override
    public Integer updateUserMessageListStatus(String driverId, String userId, Integer userListStatus) {
        Integer res = messageDao.updateUserMessageListStatus(driverId, userId, userListStatus);
        return res;
    }
}
